﻿//-----------------------------------------------------------------------
// <copyright file="RuleConfiguration.cs">
//     Copyright (c) Nicole Calinoiu. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;

namespace Bordecal.FxCop.Sdk
{
	/// <summary>
	/// Base class for rule configurations.
	/// </summary>
	public abstract class RuleConfiguration : IRuleConfiguration
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="RuleConfiguration"/> class.
		/// </summary>
		protected RuleConfiguration()
		{
		}

		/// <summary>
		/// Gets the settings as a string dictionary.
		/// </summary>
		public Dictionary<string, string> Settings { get; private set; }

		/// <summary>
		/// Initializes the instance settings dictionary from its property values.
		/// </summary>
		/// <remarks>
		/// This method should be invoked at the end of subclass constructors.
		/// </remarks>
		protected void InitializeSettings()
		{
			this.Settings = this.ToDictionary();
		}

		/// <summary>
		/// Creates a string dictionary from the current configuration instance.
		/// </summary>
		/// <returns>A <see cref="Dictionary{String, String}"/> containing the settings.</returns>
		public Dictionary<string, string> ToDictionary()
		{
			Dictionary<string, string> result = new Dictionary<string, string>();

			this.DoCopySettings(result);

			return result;
		}

		/// <summary>
		/// Copies settings from instance properties to a string dictionary.
		/// </summary>
		/// <param name="dictionary">The target settings <see cref="Dictionary{String, String}"/>.</param>
		protected abstract void DoCopySettings(Dictionary<string, string> dictionary);

		/// <summary>
		/// Initializes a configuration instance from a string dictionary.
		/// </summary>
		/// <param name="dictionary">The dictionary of property names and values.</param>
		public void LoadFromDictionary(Dictionary<string, string> dictionary)
		{
			if (dictionary == null)
			{
				throw new ArgumentNullException("dictionary");
			}

			this.DoLoadFromDictionary(dictionary);
		}

		/// <summary>
		/// Sets configuration property values from the contents of a string dictionary.
		/// </summary>
		/// <param name="dictionary">The source <see cref="Dictionary{String, String}"/>.</param>
		protected abstract void DoLoadFromDictionary(Dictionary<string, string> dictionary);
	}
}